//
//  NSString+XMXCateGory.m
//  XMXTool
//
//  Created by Xiemaoxiong on 16/3/18.
//  Copyright © 2016年 杭州犇富互联网金融服务有限公司. All rights reserved.
//

#import "NSString+XMXCateGory.h"



@implementation AttributeStyle
/// 设置富文本的样式
+(AttributeStyle *)AttributeName:(NSString *)attributeName value:(id)value range:(NSRange)range {
    AttributeStyle *style = [[self class] new];
    style.AttributeName = attributeName;
    style.value = value;
    style.range = range;
    return style;
}
@end


@implementation ForeStyle

+(ForeStyle *)withColor:(UIColor *)color range:(NSRange)range {
    ForeStyle *style = [AttributeStyle AttributeName:NSForegroundColorAttributeName value:color range:range];
    return style;
}

+(ForeStyle *)withFont:(UIFont *)font range:(NSRange)range {
    ForeStyle *style = [AttributeStyle AttributeName:NSFontAttributeName  value:font range:range];
    return style;
}

+(ForeStyle *)withDeletelineStylerange:(NSRange)range {
    
    ForeStyle *style = [AttributeStyle AttributeName:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:range];
    
    return style;
}

+(ForeStyle *)withDeletelineStyleColor:(UIColor *)color range:(NSRange)range {
    ForeStyle *style = [AttributeStyle AttributeName:NSStrikethroughColorAttributeName value:color range:range];
    
    return style;
}


@end

@implementation NSString (XMXCateGory)

/// 将字符串转换成数组
- (NSArray*)XMX_Array {
    NSData* data = [self dataUsingEncoding:NSUTF8StringEncoding];
    __autoreleasing NSError* error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error != nil) return nil;
    return result;
}

/// 获取由当前的NSString转换来的UIColor
- (UIColor*)XMX_color {
    // 判断长度先
    if (self.length < 6) return nil;
    // 去掉空格等其他字符
    NSString *cString = [[self stringByTrimmingCharactersInSet:
                          [NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    
    if ([cString length] < 6 || [cString length] > 8) return nil;
    
    static int COLOR_LENGTH = 4;
    // Alpha Red Green Blue
    unsigned int colorARGB[COLOR_LENGTH];
    for (int i = 0; i < 4; i++)
    {
        // 先初始化为所有都是255
        colorARGB[COLOR_LENGTH-i-1] = 255;
        
        // 根据子字符串进行数字转换
        NSString *subString = [cString substringFromIndex: cString.length < 2 ? 0 : cString.length - 2];
        cString = [cString substringToIndex:cString.length < 2 ? cString.length : cString.length - 2];
        if (subString.length)
        {
            [[NSScanner scannerWithString:subString] scanHexInt:&colorARGB[COLOR_LENGTH-i-1]];
        }
    }
    
    return [UIColor colorWithRed:((float) colorARGB[1] / 255.0f)
                           green:((float) colorARGB[2] / 255.0f)
                            blue:((float) colorARGB[3] / 255.0f)
                           alpha:((float) colorARGB[0] / 255.0f)];
}


/// 判断是否为正确的手机号格式
- (BOOL)XMX_isPhoneNumber {
    if ([self length]!=11) {
        return NO;
    }
    NSString *regExStr = @"^((13[0-9])|(15[^4,\\D])|(18[0-9])|(17[0-9]))\\d{8}$";
    NSError *error = NULL;
    
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regExStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSUInteger result = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, [self length])];
    if(result > 0){
        return YES;
    }else{
        return NO;
    }
}

/// 判断是否为正确的密码长度
- (BOOL)XMX_isVaildPassword {
    return [self length] >= 6 && [self length] <= 18;
}


/// 判断是否为正确的身份证
-(BOOL)XMX_isIdCard {
    if ([self length]==0) {
        return NO;
    }
    
    NSString *regExStr1 = @"^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{2}\\w$";
    NSError *error1 = NULL;
    
    NSRegularExpression *regex1 = [NSRegularExpression regularExpressionWithPattern:regExStr1
                                                                            options:NSRegularExpressionCaseInsensitive
                                                                              error:&error1];
    NSUInteger result1 = [regex1 numberOfMatchesInString:self options:0 range:NSMakeRange(0, [self length])];
    
    
    
    
    NSString *regExStr2 = @"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}\\w$";
    NSError *error2 = NULL;
    
    NSRegularExpression *regex2 = [NSRegularExpression regularExpressionWithPattern:regExStr2
                                                                            options:NSRegularExpressionCaseInsensitive
                                                                              error:&error2];
    NSUInteger result2 = [regex2 numberOfMatchesInString:self options:0 range:NSMakeRange(0, [self length])];
    
    
    
    
    if(result1 > 0||result2>0){
        return YES;
    }else{
        return NO;
    }
    
}
/// 是否未数字
- (BOOL)XMX_isNumber {
    NSString *rules = @"^[0-9]*$";
    NSPredicate *stringPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", rules];
    
    return [stringPredicate evaluateWithObject:self];
}

/// 是否为汉字
- (BOOL)XMX_isChineseCharacter {
    NSString *rules              = @"^[\u4e00-\u9fa5]{0,}$";
    NSPredicate *stringPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", rules];
    
    return [stringPredicate evaluateWithObject:self];
}

/// 创建样式
-(NSAttributedString *)XMX_createAttributeStringWithStyles:(NSArray *)styles {
    if (self.length <=0) {
        return nil;
    }
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    
    for (int count = 0; count <styles.count; count++) {
        AttributeStyle *style = styles[count];
        [attributedString addAttribute:style.AttributeName value:style.value range:style.range];
    }
    
    return attributedString;
}
/// 中文转码
- (NSString *)encodeToPercentEscapeString
{
    NSString *outputStr = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                                       NULL, /* allocator */
                                                                                       (__bridge CFStringRef)self,
                                                                                       NULL, /* charactersToLeaveUnescaped */
                                                                                       (CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);
    return outputStr;
}

/// 行高 默认:15
-(NSAttributedString *)XMX_lineHeight:(CGFloat)lineheight {
    
    NSMutableParagraphStyle *Paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    if (lineheight) {
        Paragraph.lineSpacing = lineheight;
    }else {
        Paragraph.lineSpacing = 7.5;
    }
    
    return [[NSAttributedString alloc] initWithString:self attributes:@{ NSParagraphStyleAttributeName : Paragraph}];
}

@end
